package com.base.cn.platform.os.controller.web.user.center.signin;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.user.signin.CusUserSignInService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人中心签到
 * @author ll
 * @date 2018/5/25 14:39
 */
@Controller
@RequestMapping("/web/uc")
public class WebUserSignInController extends WebBaseController {

    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private CusUserSignInService cusUserSignInService;

    /**
     * 查询用户的签到日记
     * @param request HttpServletRequest
     * @param year 年份
     * @param month 月份
     * @return Map<String,Object>
     */
    @RequestMapping("/ajax/querySignIn")
    @ResponseBody
    public Map<String,Object> querySignIn(HttpServletRequest request, @RequestParam(value = "year",required = false) String year,
                                          @RequestParam(value = "month",required = false) String month){
        BigDecimal userId= webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> map = new HashMap<>();
        if (DataUtil.idIsNotNull(userId)) {
            List<Map<String, Object>> signList = cusUserSignInService.findUserMonthSignList(userId,year,month);
            Map<String,Object> user = cusUserService.userInfo(userId,false,false,false,false,false,false,false);
            int mothsSignNum = 0;//查询月份的签到天数
            int signTotalNum = 0;//用户的签到总天数
            if (ObjectUtils.isNotEmpty(user)) {
                signTotalNum = (int)user.get("signNum");
            }
            if (ObjectUtils.isNotEmpty(signList)) {
                mothsSignNum = signList.size();
                signList.forEach(e->{
                    String signDate  = e.get("signDate").toString();
                    signDate = DateUtils.format(DateUtils.parseDate(signDate,"yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd");
                    e.put("signDate",signDate);
                });
            }
            boolean isSign = cusUserSignInService.checkUserIsSign(userId, DateUtils.format(new Date(), "yyyy-MM-dd"));
            map.put("signList", signList);
            map.put("signTotalNum", signTotalNum);
            map.put("mothsSignNum", mothsSignNum);
            map.put("isSign",isSign);
            return ResultUtil.SUCCESS("查询成功",map);
        }
        return map;
    }

    /**
     * 是否签到
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/processing/check/in")
    @ResponseBody
    public Map<String,Object> ajaxProcessingCheckIn(HttpServletRequest request){
        BigDecimal userId= webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> userSignIn = new HashMap<>();
        userSignIn.put("userId",userId);
        userSignIn.put("signDate",new Date());
        List<Map<String,Object>> userSignIns = cusUserSignInService.selectUserSignInList(userSignIn);
        int totalNum = 0;
        if (ObjectUtils.isNotEmpty(userSignIns)) {
            totalNum = userSignIns.size();
        }
        return ResultUtil.SUCCESS("查询成功",totalNum);
    }

    /**
     * 添加签到记录
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/saveSignIn")
    @ResponseBody
    public Map<String,Object> saveSignIn(HttpServletRequest request){
        BigDecimal userId= webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> userSignIn = new HashMap<>();
        userSignIn.put("userId",userId);
        userSignIn.put("signDate",new Date());
        return cusUserSignInService.saveUserSignIn(userSignIn);
    }

//=====================================================================================
}
